package at.fhj.itm;
/**
 * Simple implementation of a Queue. Provides all basic methods, to either add or delete elements.<br />
 * Methods for requesting the current status of the queue (e.g. full or not) are implemented too.
 * @author Dominik
 *
 */
public class Queue {
	
  private int maxSize; //maximum number of elements in your Queue

  private long[] queArray; //Array to store values in your Queue

  private int front; //points at the element inserted first (for removing)

  private int rear; //points at the current last element (for inserting)

  private int nItems; //current number of items in your Queue

  /**
   * Constructor of the class. 
   * @param s The maximum size you want to assign to your Queue.
   */
  public Queue(int s) {
    maxSize = s;
    queArray = new long[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
  }

  /**
   * Inserts an item into the  current last index of your Queue.<br />
   * If the queue is already full, it will insert
   * @param j The element, you want to insert.
   */
  public void insert(long j) {
    if (rear == maxSize - 1) // if Queue is full, it will again start at the first index (rear 0+1)
      rear = -1;
    queArray[++rear] = j; // increment rear and insert
    nItems++; //one more item in the Queue
  }

  /**
   * Removes the element of your Queue, which was inserted first.
   * @return The current first element of your Queue
   */
  public long remove() {
    long temp = queArray[front++]; // get value and increment front
    if (front == maxSize) // if you have already removed all elements, it will again start from the beginning (first index)
      front = 0;
    nItems--; // one less item in the Queue
    return temp;
  }

  /**
   * Returns the value, which is the first one in the Queue, but does not delete it.
   * @return First element as a <code>long</code>
   */
  public long peekFront() {
    return queArray[front];
  }

  /**
   * Checks, whether the Queue is empty or not.
   * @return true, if empty, and otherwise
   */
  public boolean isEmpty() {
    return (nItems == 0);
  }

  /**
   * Checks, whether the Queue is full or not
   * @return true, if full, and otherwise
   */
  public boolean isFull() {
    return (nItems == maxSize);
  }

  /**
   * Returns the current size of your Queue (number of elements in it).
   * @return size as <code>int</code>
   */
  public int size() {
    return nItems;
  }
}
           
         